package com.yzz.hzraisedust.hzraseidust.main;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.yzz.hzraisedust.hzraseidust.R;

/**
 * Created by zhengzhen on 2017/8/22.
 */

public class BaseActivity extends Activity{
    private Dialog mLoginingDlg; // 等待框
    private int count = 0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        super.onCreate(savedInstanceState, persistentState);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if(keyCode == KeyEvent.KEYCODE_BACK){
           finish();
           return true;
       }
        return super.onKeyDown(keyCode, event);
    }

    public void initLoginingDlg() {

        mLoginingDlg = new Dialog(this, R.style.loginingDlg);
        mLoginingDlg.setContentView(R.layout.busy_process_layout);

        Window window = mLoginingDlg.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        // 获取设备分辨率
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int cxScreen = dm.widthPixels;
        int cyScreen = dm.heightPixels;

        int height = (int) getResources().getDimension(
                R.dimen.loginingdlg_height);//
        int lrMargin = (int) getResources().getDimension(
                R.dimen.loginingdlg_lr_margin); //
        int topMargin = (int) getResources().getDimension(
                R.dimen.loginingdlg_top_margin); //

        params.y = (-(cyScreen - height) / 2) + topMargin; // -199


        params.width = cxScreen;
        params.height = height;


        mLoginingDlg.setCanceledOnTouchOutside(false);
    }


    public void showLoginingDlg() {
        count++;
        if (mLoginingDlg != null && !mLoginingDlg.isShowing()) {
            mLoginingDlg.show();
        }
    }


    public void closeLoginingDlg() {
        count --;
        if (mLoginingDlg != null && mLoginingDlg.isShowing()){
            if(count<=0) {
                mLoginingDlg.dismiss();
                count = 0;
            }
        }

    }
}
